package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.EmailAuthority;
import com.lebang.domain.SendEmail;
import com.lebang.example.SendEmailExample;
import com.lebang.feign.AppInfoFeign;
import com.lebang.model.AppInfoModel;
import com.lebang.service.EmailAuthorityService;
import com.lebang.service.sys.SendEmailService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 用户签到数据更改
 *
 * @author hwq
 * @date 2020-03-27
 */
@Controller
@RequestMapping("/sys/sendemail")
public class SendEmailController {
    @Autowired
    private SendEmailService baseService;
    @Autowired
    private AppInfoFeign appInfoFeign;
    @Autowired
    private EmailAuthorityService emailAuthorityService;

    @GetMapping("/list")
    public String list(Model model) {
        JsonData jsonData = appInfoFeign.list();
        List<AppInfoModel> appInfoModels = JSONArray.parseArray(JSONObject.toJSONString(jsonData.getData()), AppInfoModel.class);
        model.addAttribute("appInfoList", appInfoModels);
        return "/sendemail/list";
    }

    @GetMapping("/add")
    @DataSource("commonDB")
    public String add(Integer id, Model model) {
        SendEmail vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new SendEmail();
        }
        model.addAttribute("item", vo);
        return "/sendemail/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    @DataSource("commonDB")
    public JsonData edit(SendEmail vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            if (vo.getUserClass()==null){
                vo.setUserClass(0);
            }
            baseService.insertModel(vo);
        }
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/delete")
    @DataSource("commonDB")
    @ResponseBody
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        emailAuthorityService.deleteByEmailId(id);
        return Callback.success("success", "删除成功");
    }

    @PostMapping("/page")
    @ResponseBody
    @Login
    @DataSource("commonDB")
    public JSONObject newlist(Long page, Integer limit) {
        JSONObject jsonObject = new JSONObject();
        SendEmailExample example=new SendEmailExample();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        jsonObject.put("count", baseService.countByExample(example));
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectAll(page,limit));
        return jsonObject;
    }

    @Login
    @GetMapping("/app/list/{id}")
    public String role(Model model,@PathVariable Integer id) {
        JsonData jsonData = appInfoFeign.list();
        List<AppInfoModel> appInfoModels = JSONArray.parseArray(JSONObject.toJSONString(jsonData.getData()), AppInfoModel.class);
        List<Integer> list=emailAuthorityService.selectByEmailId(id);
        for (AppInfoModel appInfoModel : appInfoModels) {
            for (Integer appId : list) {
                if (appInfoModel.getId().equals(appId)){
                    appInfoModel.setSelected(1);
                    break;
                }else {
                    appInfoModel.setSelected(0);
                }
            }
            if (list.size() == 0){
                appInfoModel.setSelected(0);
            }
        }
        model.addAttribute("appInfoList", appInfoModels);
        model.addAttribute("roleid", id);
        return "/sendemail/authority";
    }

    @Login
    @PostMapping("/app/list")
    @ResponseBody
    public String authority(@RequestParam("roleid") Integer id, @RequestParam("appId") Integer appId) {
       EmailAuthority emailAuthority= emailAuthorityService.selectByIdAndAppId(id,appId);
       if (emailAuthority==null){
           emailAuthorityService.add(id,appId);
       }else {
           emailAuthorityService.updateRecord(emailAuthority);
       }
        return "OK";
    }
    @Login
    @PostMapping("/enable")
    @ResponseBody
    public JsonData enable(Integer id,String status) {
        int i=baseService.enablebyid(id,status);
        if(i>0){
            return Callback.success("200", "操作成功");
        }
        return Callback.fail("201", "操作失败");
    }
}
